package com.redis.seckill.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.redis.seckill.entity.Product;
import com.redis.seckill.mapper.ProductMapper;
import com.redis.seckill.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.redis.seckill.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @since 2020-10-28
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductService productService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 重载原生mybatis-plus的getOne方法
     * @param productId
     * @return
     */
    @Override
    public Product getOne(Integer productId) {
        if (redisUtil.hasKey("productInfo"+productId)) {
            Object o = redisUtil.get("productInfo" + productId);
            String jsonStr = JSONObject.toJSONString(o);
            return JSONObject.parseObject(jsonStr, Product.class);
        }
        return productService.getOne(new QueryWrapper<Product>().eq("product_id",productId));
    }
}
